package com.ym.pms.controller;

import com.github.pagehelper.PageHelper;
import com.ym.pms.dao.UserMapper;
import com.ym.pms.model.User;
import com.ym.pms.utils.JSONResult;
import io.swagger.annotations.*;
import io.swagger.annotations.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "用户表接口")
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @ApiOperation(value = "查询用户表",notes = "直接查询出所有的用户信息")
    @GetMapping("/queryUserList")
    public JSONResult queryUserList(){
        List<User> userList = userMapper.queryUserList();
        Map<String,Object> map = new HashMap<>();
        map.put("users",userList);
        return JSONResult.ok(map.toString());
    }

    @ApiOperation(value = "根据uId删除用户信息")
    @DeleteMapping("/deleteByPrimaryKey/{uId}")
    public JSONResult deleteByPrimaryKey(@PathVariable("uId") @ApiParam("用户ID")Integer uId){
        int result = userMapper.deleteByPrimaryKey(uId);
        if (result >= 1) {
            return JSONResult.ok("删除成功");
        } else {
            return JSONResult.errorMsg("删除失败");
        }
    }

    @ApiOperation(value = "创建新的用户信息，不可空值")
    @PostMapping("/insert")
    public JSONResult insert(User user){
        int result = userMapper.insert(user);
        if (result >= 1) {
            return JSONResult.ok("添加成功");
        } else {
            return JSONResult.errorMsg("添加失败");
        }
    }

    @ApiOperation(value = "创建新的用户信息，可空值",notes = "用于初始注册，不必填写每一项内容，uId自增可不填")
    @PostMapping("/insertSelective")
    public JSONResult insertSelective(User user){
        int result = userMapper.insertSelective(user);
        if (result >= 1) {
            return JSONResult.ok("添加成功");
        } else {
            return JSONResult.errorMsg("添加失败");
        }
    }

    @ApiOperation(value = "根据uId查询用户信息")
    @PostMapping("/selectByPrimaryKey/{uId}")
    public JSONResult selectByPrimaryKey(@PathVariable("uId") @ApiParam("用户ID")Integer uId){
        User user =  userMapper.selectByPrimaryKey(uId);
        return JSONResult.ok(user);
    }

    @ApiOperation(value = "更新用户表字段，NULL则忽略",notes = "uId为必填项且不可修改")
    @PostMapping("/updateByPrimaryKeySelective")
    public JSONResult updateByPrimaryKeySelective(User user){
        int result = userMapper.updateByPrimaryKeySelective(user);
        if (result >= 1) {
            return JSONResult.ok("修改成功");
        } else {
            return JSONResult.errorMsg("修改失败");
        }
    }

    @ApiOperation(value = "更新用户表字段，不可为NULL",notes = "uId为必填项且不可修改")
    @PostMapping("/updateByPrimaryKey")
    public JSONResult updateByPrimaryKey(User user){
        int result = userMapper.updateByPrimaryKey(user);
        if (result >= 1) {
            return JSONResult.ok("修改成功");
        } else {
            return JSONResult.errorMsg("修改失败");
        }
    }
}